//
//  KSSegmenteController.swift
//  ZYA
//
//  Created by Ken on 2019/7/26.
//  Copyright © 2019 zwb. All rights reserved.
//

import UIKit
var zoneSegVHeight:CGFloat = 40
class KSSegmenteController: UIViewController,UIScrollViewDelegate {
 
//    fileprivate var segmenteControl:UISegmentedControl!
    var zoneSegV:ZoneSegView = {
       let v = ZoneSegView()
        v.backgroundColor = .white
        v.zShowType = .all
        v.blockWidthMultipliedBy = 0.43
        v.isUserInteractionEnabled = true
        v.setCollecDataSource(arr: [])
         return v
    }()
    fileprivate lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        return scrollView
    }()
 
    var titles:[String] = []
    var vcs:[UIViewController] = []
    
  
    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
      
        
    }
    func config(titles:[String],vcs:[UIViewController]) {
        self.titles = titles
        self.vcs = vcs
        self.setUp()
    }
   
   private func setUp() {
        zoneSegV.collecSelected = { indx ,data  in
         
            let x = self.scrollView.frame.width * CGFloat(indx)
            self.scrollView.setContentOffset(CGPoint(x: x, y: 0), animated: true)
            self.showView(index: indx)
            self.ksSegmenteControllerMoveTo(index: indx)
        }
      
        
 
        edgesForExtendedLayout = UIRectEdge()
        view.backgroundColor = .white
        
        zoneSegV.frame = CGRect(x: 8, y: 0, width: KSGlobal.screenW - 16, height: zoneSegVHeight)
        var segD = [SegmenttData]()
        for i in 0..<titles.count {
            segD.append(SegmenttData(id: "\(i)",nm: titles[i]))
        }
        zoneSegV.setCollecDataSource(arr: segD)
        let height = zoneSegV.frame.maxY
        scrollView.frame = CGRect(x: 0, y: height, width: KSGlobal.screenW, height: view.ks_height - height)
        view.addSubview(zoneSegV)
//        view.addSubview(segmenteControl)
        view.addSubview(scrollView)
        
      
        
   
        for i in 0..<vcs.count {
            addChild(vcs[i])
        }
        showView(index: 0) // 默认展示第一个
        scrollView.contentSize = CGSize(width: CGFloat(vcs.count) * scrollView.frame.width, height: 0)
        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let offsetX = scrollView.contentOffset.x / KSGlobal.screenW
//        segmenteControl.selectedSegmentIndex = Int(offsetX)
        zoneSegV.selectIndx = Int(offsetX)
       showView(index: Int(offsetX))
   
       ksSegmenteControllerMoveTo(index: Int(offsetX))
        
    }
    func showView(index:Int){
        
        let subview = vcs[index].view!
        guard subview.isDescendant(of: scrollView) else {
            subview.frame = CGRect(x: CGFloat(index) * view.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
            scrollView.addSubview(subview)
            return
        }
   
    }
    
    func ksSegmenteControllerMoveTo(index:Int){
        
    }
 
}

